﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Members;
using VK.Comm;
using VK.Casemeet.Lines;

namespace VK.Casemeet.Web.Front
{
    public partial class SubmitReserve : System.Web.UI.Page
    {
        public int lineID = 0;
        public int area1, area2, area3;
        public string strMessage = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            lineID = Util.GetRouteDataInt("LineID");

            if (!IsPostBack)
            {
                MemberHelper.BindSexRad(radSex);

                LoadData();
            }
        }

        internal void LoadData()
        {
            if (lineID != 0)
            {
                MemberEntity member = MemberBLL.GetMember(MemberBLL.GetSessionMember().MemberID);
                txtName.Text = member.Name;
                radSex.SelectedValue = member.Sex.ToString();
                txtQq.Text = member.QQ;
                txtPhone.Text = member.Phone;
                area1 = member.ProvinceID;
                area2 = member.CityID;
                area3 = member.AreaID;
                txtAddress.Text = member.Address;
            }
        }

        protected void butSubmit_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length == 0)
            {
                strMessage += "<p>真实姓名不能为空!</p>";
            }
            if (radSex.SelectedValue.Length == 0)
            {
                strMessage += "<p>请选择性别!</p>";
            }
            int provinceID = Util.GetFormInt("ddlArea1");
            if (provinceID == 0)
            {
                strMessage += "<p>请选择省!</p>";
            }
            int cityID = Util.GetFormInt("ddlArea2");
            if (cityID == 0)
            {
                strMessage += "<p>请选择市!</p>";
            }
            int areaID = Util.GetFormInt("ddlArea3");
            if (areaID == 0)
            {
                strMessage += "<p>请选择区!</p>";
            }
            if (txtPhone.Text.Length == 0)
            {
                strMessage += "<p>联系电话不能为空!</p>";
            }
            if (txtAddress.Text.Length == 0)
            {
                strMessage += "<p>通讯地址不能为空!</p>";
            }

            if (!string.IsNullOrEmpty(strMessage))
            {
                strMessage = "<dd id='vcodeMsg' class='loginMsg'>" + strMessage;
                strMessage = strMessage + "</dd>";
                return;
            }

            MemberEntity member = LoadEntity();
            member.MemberID = MemberBLL.GetSessionMember().MemberID;

            if (MemberBLL.UpdateMember(member) > 0)
            {
                LineMemberEntity reserveGoods = new LineMemberEntity();
                reserveGoods.LineID = lineID;
                reserveGoods.MemberID = member.MemberID;
                reserveGoods.Created = DateTime.Now;
                if (LineMemberBLL.InsertLineMember(reserveGoods) > 0)
                {
                    HttpContext.Current.Response.Write(Util.WrapJavaScript(
                        " parent.parent.isenternum=1;" +
                        " parent.parent.document.getElementById('floatBoxBg').style.display='none';" +
                        " parent.parent.document.getElementById('floatBox').style.display='none';" +
                        " parent.parent.document.getElementById('contentBody_butReserve').innerHTML='已报名'; "));
                }
            }
            else
            {
                strMessage = "<dd id='vcodeMsg' class='regMsg'><p>提交信息失败，请联系管理员!</p></dd>";
            }
        }

        protected MemberEntity LoadEntity()
        {
            MemberEntity member = MemberBLL.GetMember(MemberBLL.GetSessionMember().MemberID);
            member.Name = txtName.Text;
            member.Sex = int.Parse(radSex.SelectedValue);
            member.QQ = txtQq.Text;
            member.Phone = txtPhone.Text;
            member.ProvinceID = Util.GetFormInt("ddlArea1");
            member.CityID = Util.GetFormInt("ddlArea2");
            member.AreaID = Util.GetFormInt("ddlArea3");
            member.Address = txtAddress.Text;

            return member;
        }
    }
}